<?php

namespace app\common\model;

use think\Db;
use think\Model;


class SaleItem extends Model
{

    

    

    // 表名
    protected $name = 'sale_item';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'create_time';
    protected $updateTime = false;
    protected $deleteTime = false;

    // 追加属性
    protected $append = [

    ];
	
    
    public function user() {
    	return $this->belongsTo('user', 'user_id', 'id', '', 'left')->setEagerlyType(0);
    }
	
	public function promotion() {
		return $this->belongsTo('promotion', 'promotion_id', 'id', '', 'left')->setEagerlyType(0);
	}
	
	public function agent() {
		return $this->belongsTo('agentShop', 'agent_id', 'id', '', 'left')->setEagerlyType(0);
	}
	
	/**
	 * 添加销量记录
	 * @param $orderId 订单ID
	 * @param $userId 用户ID
	 * @param $number 数量
	 * @param string $agentId 下级代理ID
	 * @param string $promotionId 代言人ID
	 * @return int|string
	 */
	public static function addRow($orderId, $userId, $number, $agentId = '', $promotionId = '') {
		return Db::name('saleItem')
			->insert([
				'order_id' => $orderId,
				'user_id' => $userId,
				'number' => $number,
				'agent_id' => $agentId,
				'promotion_id' => $promotionId,
				'create_time' => time()
			]);
	}





}
